package cn.lhq.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.lhq.bean.ModifyCountInts;
import cn.lhq.bean.Vote;
import cn.lhq.bean.VoteIdAndCount;
import cn.lhq.bean.VoteResult;
import cn.lhq.dao.VoteDao;
@Service
public class VoteServiceImpl implements VoteService{
	@Resource
	private VoteDao voteDao;

	public VoteResult findByActivityId(int activityId) throws Exception {
		List<Vote> list = voteDao.findByActivityId(activityId);
		VoteResult result = new VoteResult();
		if(null == list){
			result.setStatus(1);
			result.setMsg("没有该活动的选项");
			return result;
		}
		result.setStatus(0);
		result.setMsg("选项查询成功");
		result.setData(list);
		return result;
	}

	public VoteResult modifyVoteCount(ModifyCountInts ints) throws Exception {		
		VoteResult result = new VoteResult();
		for(int i=0; i<ints.getInts().length; i=i+2){
			Vote vote = voteDao.findById(ints.getInts()[i]);
			vote.setCount(ints.getInts()[i+1]);
			voteDao.update(vote);
		}		
		result.setStatus(0);
		result.setMsg("修改成功");
		return result;
	}

	public VoteResult addCount(int id) throws Exception {
		Vote vote = voteDao.findById(id);
		int count = vote.getCount();
		vote.setCount(count+1);
		voteDao.update(vote);
		VoteResult result = new VoteResult();
		result.setStatus(0);
		result.setMsg("票数修改成功");
		return result;
	}

	public VoteResult findById(int id) throws Exception {
		Vote vote = voteDao.findById(id);
		VoteResult result = new VoteResult();
		if(null == vote){
			result.setStatus(1);
			result.setMsg("没有选项");
			return result;
		}
		result.setStatus(0);
		result.setMsg("选项查询成功");
		result.setData(vote);
		return result;
	}

}
